*********************************************************************
***      IDENTIFY LONG OUTPATIENT STAYS			    			  ***
*** Identify long outpatient stays -- these are stays that could  ***
*** be for observation services, but just aren't coded as that    ***
*** Long outpatient stay: an outpatient claim at hospital that    ***
*** has LOS == 1. 												  ***
*** Last edited: 12/2/2022                       				  ***
*********************************************************************

ssc install mipolate

log using "${CodePath}/log/identify_longoutpatient_$S_DATE.log", text replace
// Switches
local byyear = 1
local append = 1
*--------------------------------------------------
* Identifying long outpatient stays and whether they originated in the ED in outpatient revenue files
*--------------------------------------------------
if `byyear' == 1{
	forval year = 2007/2015{
		di "Outpatient - `year'"
		use bene_id clm_id from_dt thru_dt provider pmt_amt fac_type using "${McareDataPath}/harm/100pct/op/`year'/opc`year'.dta", clear

		// restrict to hospitals only
		keep if fac_type == "1"

		// calculate LOS in hospital, then only keep the ones that could be 'long outpatient stays'
			// outpatient claims with long LOS likely indicate recurring visits, not actual long stays
		gen los = thru_dt - from_dt
		keep if los > 0 & los <= 3

		// merge in info from revenue file
		merge 1:m clm_id using "${McareDataPath}/harm/100pct/op/`year'/opr`year'.dta", keepusing(rev_cntr hcpcs_cd rev_dt rev_chrg ) nogen keep(1 3)

		di "creating dummy for ED charges in claim..."
		gen temp_ED = rev_cntr == "0450" | rev_cntr == "0451" | rev_cntr == "0452" | rev_cntr == "0453" | rev_cntr == "0454" | rev_cntr == "0455" | rev_cntr == "0456" | rev_cntr == "0457" | rev_cntr == "0458" | rev_cntr == "0459" | ///
			hcpcs_cd == "99281" | hcpcs_cd == "99282" | hcpcs_cd == "99283" | hcpcs_cd == "99284" | hcpcs_cd == "99285" 
		bys clm_id: egen any_ED = max(temp_ED)
		drop temp_ED

		di "creating dummy for evidence of obs in claim..."
		gen temp_obs = rev_cntr == "0760" | rev_cntr == "0762" | hcpcs_cd == "G0378" | hcpcs_cd == "G0379" ///
			| hcpcs_cd == "99217" | hcpcs_cd == "99218" | hcpcs_cd == "99219" | hcpcs_cd == "99220" | hcpcs_cd == "99234" | hcpcs_cd == "99235"| hcpcs_cd == "99236"
		bys clm_id: egen any_obs = max(temp_obs)
		drop temp_obs

		di "drop anything with evidence of obs..."
		drop if any_obs
		drop any_obs 

		// count each claim only once
		bys clm_id: gen n = _n
		keep if n == 1

		gen file_year = `year' 

		di "merging in bsfab..."
		merge m:1 bene_id using "${McareDataPath}/harm/100pct/bsfab/`year'/bsfab`year'.dta", keepusing(hmoind*) nogen keep(1 3)

		di "keeping only ffs patient"
		// Keep only if FFS patient in that month
		gen disc_mon = month(thru_dt)
		keep if (disc_mon == 1 & hmoind01 == "0" | disc_mon == 2 & hmoind02 == "0" | disc_mon == 3 & hmoind03 == "0" | disc_mon == 4 & hmoind04 == "0" | disc_mon == 5 & hmoind05 == "0" | disc_mon == 6 & hmoind06 == "0" | disc_mon == 7 & hmoind07 == "0" | disc_mon == 8 & hmoind08 == "0" | disc_mon == 9 & hmoind09 == "0" | disc_mon == 10 & hmoind10 == "0" | disc_mon == 11 & hmoind11 == "0" |  disc_mon == 12 & hmoind12 == "0" )
		drop disc_mon hmoind* thru_dt

		save "${DataPath}/observation/longopED_`year'.dta", replace 
			
	}   //forval year
} // byyear



*--------------------------------------------------
* Combine and collapse
*--------------------------------------------------
if `append' == 1{
	forval year = 2007/2014{
		use "${DataPath}/observation/longopED_`year'.dta", clear
		gen year = `year'
		gen longop = 1
		bys clm_id: gen clm_pmt_amt = pmt_amt if _n == 1
		replace clm_pmt_amt = 0 if missing(clm_pmt_amt)

		preserve
			keep if los == 1
			gcollapse (sum) longop = longop longop_clm_pmt_amt = clm_pmt_amt, by(provider year)
			rename provider prvdrnum

			save "${DataPath}/observation/longop_claimlevel_`year'.dta", replace
		restore

		// restrict to observation stays with ED
		keep if any_ED == 1

		// long outpatient stays with los == 1
		preserve
			keep if los == 1
			gcollapse (sum) longop = longop longop_clm_pmt_amt = clm_pmt_amt, by(provider year)
			rename provider prvdrnum

			save "${DataPath}/observation/longopED_claimlevel_`year'.dta", replace
		restore
	} //forval year


	// append long op
	use "${DataPath}/observation/longop_claimlevel_2007.dta", clear
	forval year = 2008/2014{
		append using "${DataPath}/observation/longop_claimlevel_`year'.dta"
	} //forval year
	save "${DataPath}/observation/longop_claimlevel_0715_year.dta", replace


	// append long op from ED
	use "${DataPath}/observation/longopED_claimlevel_2007.dta", clear

	forval year = 2008/2014{
		append using "${DataPath}/observation/longopED_claimlevel_`year'.dta"
	} //forval year
	save "${DataPath}/observation/longopED_claimlevel_0715_year.dta", replace

} //append = 1



log close



